package com.java110.community.cmd.ownerRepair;

import com.alibaba.fastjson.JSONObject;
import com.java110.community.cmd.community.UpdateCommunityCmd;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairStaffsFlow;
import com.java110.dto.repair.RepairStateEnum;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.Map;

@Java110Cmd(serviceCode = "ownerRepair.repairStop")
public class RepairStopCmd extends Cmd {
    private static Logger logger = LoggerFactory.getLogger(UpdateCommunityCmd.class);
    @Autowired
    private IRepairInnerServiceSMO repairInnerService;
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        logger.info("保存保修配置请求入参：{}", reqJson.toJSONString());
        //暂停保修
        repairInnerService.repairStop(reqJson);
        //添加流转状态
        //查出上一个流转单，修改结束时间
        RepairStaffsFlow repairStaffsFlows = repairInnerService.lastTimeRepairFlow(reqJson.getLong("repairId"));
        repairStaffsFlows.setEndTime(DateUtil.getCurrentDate());
        repairStaffsFlows.setTimeConsuming(System.currentTimeMillis()-repairStaffsFlows.getCreatTime().getTime());
        repairInnerService.updateRepairDispatchFlow(repairStaffsFlows);
        //添加当前流转单
        repairStaffsFlows.setId(null);
        repairStaffsFlows.setEndTime(null);
        repairStaffsFlows.setCreatTime(DateUtil.getCurrentDate());
        repairStaffsFlows.setTimeConsuming(null);
        repairStaffsFlows.setState(Integer.parseInt(RepairStateEnum.STOP.getState()));
        repairStaffsFlows.setStateName(RepairStateEnum.STOP.getStateName());
        repairStaffsFlows.setContext(reqJson.getString("remark"));
        JSONObject jsonObject = BeanConvertUtil.beanCovertJson(repairStaffsFlows);
        jsonObject.put("createUserName",reqJson.get("userName"));
        jsonObject.put("currentUserId",reqJson.get("userId"));
        repairInnerService.repairDispatchFlow(jsonObject);
    }
}
